package com.nwpusp.display.views.components;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;

import com.example.petssports.R;
import com.nwpusp.display.GlobalVar;


public class Animal extends View{
	
	public Bitmap run_1;
	public Bitmap run_2;
	public Bitmap jump_1;
	public Bitmap jump_2;
	public Bitmap jump_3;
	public Bitmap jump_4;
	public Bitmap jump_5;
	public Bitmap run_1c;
	public Bitmap run_2c;
	public Bitmap jump_1c;
	public Bitmap jump_2c;
	public Bitmap jump_3c;
	public Bitmap jump_4c;
	public Bitmap jump_5c;
	public Bitmap down_1;
	public Bitmap down_2;
	public Bitmap down_3;
	public Bitmap down_4;
	public Bitmap down_5;
	public Bitmap down_6;
	public Bitmap down_1c;
	public Bitmap down_2c;
	public Bitmap down_3c;
	public Bitmap down_4c;
	public Bitmap down_5c;
	public Bitmap down_6c;
	
	public Bitmap nameBarBitmap;
	
	public Bitmap rtnBitmap;
	public float x;
	public float y;
	public float newHeight;
	public int headerIndex;
	public float AnimalNormalWidth;
	public float AnimalNormalHeight;
	public float AnimalDownHeight;
	public float nameBarWidth;
	public float nameBarHeight;
	public int index;
	public int status;
	private static final int status_normal = 0;
	private static final int status_waiting = 1;
	private static final int status_jump = 2;
	private static final int status_down = 3;
	private static final int status_block = 4;
	private static final int status_end = 5;
	private String name = null;
	private int run_temp = 0;
	private int jump_temp = 0;
	private int down_temp = 0;
	
	public Animal(Context view,float newHeight, int index, int status, String name) {
		super(view);
		this.name = name;
		this.newHeight = newHeight;
		this.index = index;
		this.status = status;
		getBitmapSource();
		
		nameBarBitmap = DrawNameBar(nameBarBitmap, name);
		nameBarWidth = nameBarBitmap.getWidth();
		nameBarHeight = nameBarBitmap.getHeight();
		
		run_1 = reBitmap(run_1,newHeight);
		run_2 = reBitmap(run_2,newHeight);
		run_1c = GlobalVar.CombinationBitmap(nameBarBitmap,run_1);
		run_2c = GlobalVar.CombinationBitmap(nameBarBitmap,run_2);
		AnimalNormalWidth = run_1.getWidth();
		AnimalNormalHeight = run_1.getHeight();
		
		jump_1 = reBitmap(jump_1,newHeight);
		jump_2 = reBitmap(jump_2,newHeight);
		jump_3 = reBitmap(jump_3,newHeight);
		jump_4 = reBitmap(jump_4,newHeight);
		jump_5 = reBitmap(jump_5,newHeight);
		jump_1c = GlobalVar.CombinationBitmap(nameBarBitmap,jump_1);
		jump_2c = GlobalVar.CombinationBitmap(nameBarBitmap,jump_2);
		jump_3c = GlobalVar.CombinationBitmap(nameBarBitmap,jump_3);
		jump_4c = GlobalVar.CombinationBitmap(nameBarBitmap,jump_4);
		jump_5c = GlobalVar.CombinationBitmap(nameBarBitmap,jump_5);
		
		down_1 = reBitmap(down_1,newHeight*(float)0.8);
		down_2 = reBitmap(down_2,newHeight*(float)0.8);
		down_3 = reBitmap(down_3,newHeight*(float)0.8);
		down_4 = reBitmap(down_4,newHeight*(float)0.8);
		down_5 = reBitmap(down_5,newHeight*(float)0.8);
		down_6 = reBitmap(down_6,newHeight*(float)0.8);
		down_1c = GlobalVar.CombinationBitmap(nameBarBitmap,down_1);
		down_2c = GlobalVar.CombinationBitmap(nameBarBitmap,down_2);
		down_3c = GlobalVar.CombinationBitmap(nameBarBitmap,down_3);
		down_4c = GlobalVar.CombinationBitmap(nameBarBitmap,down_4);
		down_5c = GlobalVar.CombinationBitmap(nameBarBitmap,down_5);
		down_6c = GlobalVar.CombinationBitmap(nameBarBitmap,down_6);
		AnimalDownHeight = down_3.getHeight();
	}
	
	public String getName() {
		return this.name;
	}
	
	public int getStatus() {
		return this.status;
	}
	
	private Bitmap reBitmap(Bitmap bitmap,float newHeight){
		bitmap = GlobalVar.VerticalScaleFixHeight(bitmap,newHeight);
		return bitmap;
	}
	
	public void setLocation(float x ,float y){
		this.x = x;
		if(status!=status_down)
			this.y = y - nameBarHeight;
		else {
			this.y = y - nameBarHeight + AnimalNormalHeight - AnimalDownHeight;
		}
	}
	public void setStatus(int status){
		this.status = status;
	}
	
	private void getBitmapSource(){
		switch(index){
			case 1:{
				run_1 = BitmapFactory.decodeResource(getResources(), R.drawable.xiong_run_1);
				run_2 = BitmapFactory.decodeResource(getResources(), R.drawable.xiong_run_2);
				jump_1 = BitmapFactory.decodeResource(getResources(), R.drawable.xiong_kua_1);
				jump_2 = BitmapFactory.decodeResource(getResources(), R.drawable.xiong_kua_2);
				jump_3 = BitmapFactory.decodeResource(getResources(), R.drawable.xiong_kua_3);
				jump_4 = BitmapFactory.decodeResource(getResources(), R.drawable.xiong_kua_4);
				jump_5 = BitmapFactory.decodeResource(getResources(), R.drawable.xiong_kua_5);
				down_1 = BitmapFactory.decodeResource(getResources(), R.drawable.xiong_zuan_1);
				down_2 = BitmapFactory.decodeResource(getResources(), R.drawable.xiong_zuan_2);
				down_3 = BitmapFactory.decodeResource(getResources(), R.drawable.xiong_zuan_3);
				down_4 = BitmapFactory.decodeResource(getResources(), R.drawable.xiong_zuan_4);
				down_5 = BitmapFactory.decodeResource(getResources(), R.drawable.xiong_zuan_5);
				down_6 = BitmapFactory.decodeResource(getResources(), R.drawable.xiong_zuan_6);
				break;
			}
			case 2:{
				run_1 = BitmapFactory.decodeResource(getResources(), R.drawable.niu_run_1);
				run_2 = BitmapFactory.decodeResource(getResources(), R.drawable.niu_run_2);
				jump_1 = BitmapFactory.decodeResource(getResources(), R.drawable.niu_kua_1);
				jump_2 = BitmapFactory.decodeResource(getResources(), R.drawable.niu_kua_2);
				jump_3 = BitmapFactory.decodeResource(getResources(), R.drawable.niu_kua_3);
				jump_4 = BitmapFactory.decodeResource(getResources(), R.drawable.niu_kua_4);
				jump_5 = BitmapFactory.decodeResource(getResources(), R.drawable.niu_kua_5);
				down_1 = BitmapFactory.decodeResource(getResources(), R.drawable.niu_zuan_1);
				down_2 = BitmapFactory.decodeResource(getResources(), R.drawable.niu_zuan_2);
				down_3 = BitmapFactory.decodeResource(getResources(), R.drawable.niu_zuan_3);
				down_4 = BitmapFactory.decodeResource(getResources(), R.drawable.niu_zuan_4);
				down_5 = BitmapFactory.decodeResource(getResources(), R.drawable.niu_zuan_5);
				down_6 = BitmapFactory.decodeResource(getResources(), R.drawable.niu_zuan_6);
				break;
			}
			case 3:{
				run_1 = BitmapFactory.decodeResource(getResources(), R.drawable.ji_run_1);
				run_2 = BitmapFactory.decodeResource(getResources(), R.drawable.ji_run_2);
				jump_1 = BitmapFactory.decodeResource(getResources(), R.drawable.ji_kua_1);
				jump_2 = BitmapFactory.decodeResource(getResources(), R.drawable.ji_kua_2);
				jump_3 = BitmapFactory.decodeResource(getResources(), R.drawable.ji_kua_3);
				jump_4 = BitmapFactory.decodeResource(getResources(), R.drawable.ji_kua_4);
				jump_5 = BitmapFactory.decodeResource(getResources(), R.drawable.ji_kua_5);
				down_1 = BitmapFactory.decodeResource(getResources(), R.drawable.ji_zuan_1);
				down_2 = BitmapFactory.decodeResource(getResources(), R.drawable.ji_zuan_2);
				down_3 = BitmapFactory.decodeResource(getResources(), R.drawable.ji_zuan_3);
				down_4 = BitmapFactory.decodeResource(getResources(), R.drawable.ji_zuan_4);
				down_5 = BitmapFactory.decodeResource(getResources(), R.drawable.ji_zuan_5);
				down_6 = BitmapFactory.decodeResource(getResources(), R.drawable.ji_zuan_6);
				break;
			}
			case 4:{
				run_1 = BitmapFactory.decodeResource(getResources(), R.drawable.tu_run_1);
				run_2 = BitmapFactory.decodeResource(getResources(), R.drawable.tu_run_2);
				jump_1 = BitmapFactory.decodeResource(getResources(), R.drawable.tu_kua_1);
				jump_2 = BitmapFactory.decodeResource(getResources(), R.drawable.tu_kua_2);
				jump_3 = BitmapFactory.decodeResource(getResources(), R.drawable.tu_kua_3);
				jump_4 = BitmapFactory.decodeResource(getResources(), R.drawable.tu_kua_4);
				jump_5 = BitmapFactory.decodeResource(getResources(), R.drawable.tu_kua_5);
				down_1 = BitmapFactory.decodeResource(getResources(), R.drawable.tu_zuan_1);
				down_2 = BitmapFactory.decodeResource(getResources(), R.drawable.tu_zuan_2);
				down_3 = BitmapFactory.decodeResource(getResources(), R.drawable.tu_zuan_3);
				down_4 = BitmapFactory.decodeResource(getResources(), R.drawable.tu_zuan_4);
				down_5 = BitmapFactory.decodeResource(getResources(), R.drawable.tu_zuan_5);
				down_6 = BitmapFactory.decodeResource(getResources(), R.drawable.tu_zuan_6);
				break;
			}
			case 5:{
				run_1 = BitmapFactory.decodeResource(getResources(), R.drawable.zhu_run_1);
				run_2 = BitmapFactory.decodeResource(getResources(), R.drawable.zhu_run_2);
				jump_1 = BitmapFactory.decodeResource(getResources(), R.drawable.zhu_kua_1);
				jump_2 = BitmapFactory.decodeResource(getResources(), R.drawable.zhu_kua_2);
				jump_3 = BitmapFactory.decodeResource(getResources(), R.drawable.zhu_kua_3);
				jump_4 = BitmapFactory.decodeResource(getResources(), R.drawable.zhu_kua_4);
				jump_5 = BitmapFactory.decodeResource(getResources(), R.drawable.zhu_kua_5);
				down_1 = BitmapFactory.decodeResource(getResources(), R.drawable.zhu_zuan_1);
				down_2 = BitmapFactory.decodeResource(getResources(), R.drawable.zhu_zuan_2);
				down_3 = BitmapFactory.decodeResource(getResources(), R.drawable.zhu_zuan_3);
				down_4 = BitmapFactory.decodeResource(getResources(), R.drawable.zhu_zuan_4);
				down_5 = BitmapFactory.decodeResource(getResources(), R.drawable.zhu_zuan_5);
				down_6 = BitmapFactory.decodeResource(getResources(), R.drawable.zhu_zuan_6);
				break;
			}
			case 6:{
				run_1 = BitmapFactory.decodeResource(getResources(), R.drawable.yang_run_1);
				run_2 = BitmapFactory.decodeResource(getResources(), R.drawable.yang_run_2);
				jump_1 = BitmapFactory.decodeResource(getResources(), R.drawable.yang_kua_1);
				jump_2 = BitmapFactory.decodeResource(getResources(), R.drawable.yang_kua_2);
				jump_3 = BitmapFactory.decodeResource(getResources(), R.drawable.yang_kua_3);
				jump_4 = BitmapFactory.decodeResource(getResources(), R.drawable.yang_kua_4);
				jump_5 = BitmapFactory.decodeResource(getResources(), R.drawable.yang_kua_5);
				down_1 = BitmapFactory.decodeResource(getResources(), R.drawable.yang_zuan_1);
				down_2 = BitmapFactory.decodeResource(getResources(), R.drawable.yang_zuan_2);
				down_3 = BitmapFactory.decodeResource(getResources(), R.drawable.yang_zuan_3);
				down_4 = BitmapFactory.decodeResource(getResources(), R.drawable.yang_zuan_4);
				down_5 = BitmapFactory.decodeResource(getResources(), R.drawable.yang_zuan_5);
				down_6 = BitmapFactory.decodeResource(getResources(), R.drawable.yang_zuan_6);
				break;
			}
			default:break;
		
		}
		
		nameBarBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.gameviewnamebar);
	}

	public Bitmap draw(long num){	
		//Log.d("aob_ipmsg", String.valueOf(status));
		if(status==status_normal){
			run_temp = (int)num%2;
			jump_temp = 0;
			down_temp = 0;
			switch(run_temp){
				case 0:{
					rtnBitmap = run_1c;
					break;
				}
				case 1:{
					rtnBitmap = run_2c;
					break;
				}
				default:break;
			}
		}
		else if(status == status_waiting){
			rtnBitmap = run_1c;
		}
		else if(status == status_jump) {
			switch(jump_temp){
				case 0 :{
					rtnBitmap = jump_1c;
					jump_temp ++;
					break;
				}
				case 1 :{
					rtnBitmap = jump_1c;
					jump_temp ++;
					break;
				}
				case 2 :{
					rtnBitmap = jump_2c;
					jump_temp ++;
					break;
				}
				case 3 :{
					rtnBitmap = jump_2c;
					jump_temp ++;
					break;
				}
				case 4 :{
					rtnBitmap = jump_3c;
					jump_temp ++;
					break;
				}
				case 5 :{
					rtnBitmap = jump_3c;
					jump_temp ++;
					break;
				}
				case 6 :{
					rtnBitmap = jump_4c;
					jump_temp ++;
					break;
				}
				case 7 :{
					rtnBitmap = jump_4c;
					jump_temp ++;
					break;
				}
				case 8 :{
					rtnBitmap = jump_5c;
					break;
				}
				default:break;
			}
		}
		else if(status == status_down) {
			switch(down_temp){
				case 0 :{
					rtnBitmap = down_1c;
					down_temp ++;
					break;
				}
				case 1 :{
					rtnBitmap = down_1c;
					down_temp ++;
					break;
				}
				case 2 :{
					rtnBitmap = down_2c;
					down_temp ++;
					break;
				}
				case 3 :{
					rtnBitmap = down_2c;
					down_temp ++;
					break;
				}
				case 4 :{
					rtnBitmap = down_3c;
					down_temp ++;
					break;
				}
				case 5 :{
					rtnBitmap = down_3c;
					down_temp ++;
					break;
				}
				case 6 :{
					rtnBitmap = down_4c;
					down_temp ++;
					break;
				}
				case 7 :{
					rtnBitmap = down_4c;
					down_temp ++;
					break;
				}
				case 8 :{
					rtnBitmap = down_5c;
					break;
				}
				case 9 :{
					rtnBitmap = down_5c;
					break;
				}
				case 10 :{
					rtnBitmap = down_6c;
					break;
				}
				default:break;
			}
		}
		else if(status == status_block){
			rtnBitmap = run_1c;
		}
		
		return rtnBitmap;
	}
	
	private Bitmap DrawNameBar( Bitmap SourceBitmap, String nameStr) {
		Paint fontPaint = new Paint();
		fontPaint.setTextSize(GlobalVar.screenHeight/24);
		fontPaint.setColor(0xFFE7019C);
		fontPaint.setAntiAlias(true);
		float strWidth =  fontPaint.measureText(nameStr);
		float strHeight =  (-fontPaint.getFontMetrics().descent - fontPaint.getFontMetrics().ascent);
		int width = (int)(strWidth*(float)1.5);
		int height = (int)(strHeight*(float)2);
		SourceBitmap = Scale(SourceBitmap,width,height);
		Bitmap NewBitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas();
		canvas.setBitmap(NewBitmap);
		canvas.drawBitmap(SourceBitmap, 0, 0, null);
		canvas.drawText(nameStr, (width - strWidth) / 2, (height + strHeight) / 2, fontPaint);
		return NewBitmap;
	}
	
	private Bitmap Scale(Bitmap bitmap,int newWidth,int newHeight){
		Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap,newWidth,newHeight,true); 
	    return resizedBitmap;
	}
	
	private  Bitmap accelerate(Bitmap sourceBitmap) {
		Paint paint = new Paint();
		paint.setAlpha(30);
		Paint paint2 = new Paint();
		paint2.setAlpha(50);
		Paint paint3 = new Paint();
		paint3.setAlpha(100);
		Bitmap NewBitmap = Bitmap.createBitmap((int)(rtnBitmap.getWidth()*1.5),rtnBitmap.getHeight(), Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas();
		canvas.setBitmap(NewBitmap);
		canvas.drawBitmap(rtnBitmap, rtnBitmap.getWidth()*(float)0.5, 0, null);
		canvas.drawBitmap(sourceBitmap, 0, nameBarHeight, paint);
		canvas.drawBitmap(sourceBitmap, rtnBitmap.getWidth()*(float)0.2, nameBarHeight, paint2);
		canvas.drawBitmap(sourceBitmap, rtnBitmap.getWidth()*(float)0.4, nameBarHeight, paint3);
		return NewBitmap;
	}

	public int getIndex() {
		return this.index;
	}

}
